package com.dengyuanke.upms.logic.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dengyuanke.common.core.constant.CommonConstants;
import com.dengyuanke.common.core.result.R;
import com.dengyuanke.common.log.annotation.SysLog;
import com.dengyuanke.common.security.util.SecurityUtils;
import com.dengyuanke.upms.domain.dto.MenuTree;
import com.dengyuanke.upms.domain.dto.TreeUtil;
import com.dengyuanke.upms.domain.entity.Menu;
import com.dengyuanke.upms.logic.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2019-06-29
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    /**
     * 返回当前用户的树形菜单集合
     *
     * @return 当前用户的树形菜单
     */
    @GetMapping
    public R getUserMenu() throws IOException {
        List<Long> roles = SecurityUtils.getRoles();
        Set<Menu> menus = new HashSet<>();
        roles.forEach(roleId -> {
            List<Menu> menuList = menuService.findMenuByRoleId(roleId);
            menus.addAll(menuList);
        });
        List<MenuTree> menuTrees = menus.stream()
                .map(MenuTree::new)
                .sorted(Comparator.comparingInt(MenuTree::getSort))
                .collect(Collectors.toList());
        return R.success(TreeUtil.build(menuTrees, -1L));
//        String json = "[{\"id\":1000,\"parentId\":-1,\"children\":[{\"id\":1100,\"parentId\":1000,\"children\":[],\"icon\":\"icon-yonghuguanli\",\"name\":\"用户管理\",\"spread\":false,\"path\":\"/admin/user/index\",\"keepAlive\":\"1\",\"code\":null,\"type\":\"0\",\"label\":\"用户管理\",\"sort\":1},{\"id\":1200,\"parentId\":1000,\"children\":[],\"icon\":\"icon-caidanguanli\",\"name\":\"菜单管理\",\"spread\":false,\"path\":\"/admin/menu/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"菜单管理\",\"sort\":2},{\"id\":1300,\"parentId\":1000,\"children\":[],\"icon\":\"icon-jiaoseguanli\",\"name\":\"角色管理\",\"spread\":false,\"path\":\"/admin/role/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"角色管理\",\"sort\":3},{\"id\":1400,\"parentId\":1000,\"children\":[],\"icon\":\"icon-web-icon-\",\"name\":\"部门管理\",\"spread\":false,\"path\":\"/admin/dept/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"部门管理\",\"sort\":4},{\"id\":1500,\"parentId\":1000,\"children\":[],\"icon\":\"icon-erji-zuhushouye\",\"name\":\"租户管理\",\"spread\":false,\"path\":\"/admin/tenant/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"租户管理\",\"sort\":5}],\"icon\":\"icon-quanxianguanli\",\"name\":\"权限管理\",\"spread\":false,\"path\":\"/user\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"权限管理\",\"sort\":0},{\"id\":2000,\"parentId\":-1,\"children\":[{\"id\":2100,\"parentId\":2000,\"children\":[],\"icon\":\"icon-rizhiguanli\",\"name\":\"日志管理\",\"spread\":false,\"path\":\"/admin/log/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"日志管理\",\"sort\":5},{\"id\":10000,\"parentId\":2000,\"children\":[],\"icon\":\"icon-wenjianguanli\",\"name\":\"文件管理\",\"spread\":false,\"path\":\"/admin/file/index\",\"keepAlive\":\"1\",\"code\":null,\"type\":\"0\",\"label\":\"文件管理\",\"sort\":6},{\"id\":2200,\"parentId\":2000,\"children\":[],\"icon\":\"icon-navicon-zdgl\",\"name\":\"字典管理\",\"spread\":false,\"path\":\"/admin/dict/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"字典管理\",\"sort\":6},{\"id\":2210,\"parentId\":2000,\"children\":[],\"icon\":\"icon-canshu\",\"name\":\"参数管理\",\"spread\":false,\"path\":\"/admin/param/index\",\"keepAlive\":\"1\",\"code\":null,\"type\":\"0\",\"label\":\"参数管理\",\"sort\":7},{\"id\":2800,\"parentId\":2000,\"children\":[],\"icon\":\"icon-guanwangfangwen\",\"name\":\"Quartz管理\",\"spread\":false,\"path\":\"/daemon/job-manage/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"Quartz管理\",\"sort\":8},{\"id\":2300,\"parentId\":2000,\"children\":[],\"icon\":\"icon-weibiaoti46\",\"name\":\"代码生成\",\"spread\":false,\"path\":\"/gen/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"代码生成\",\"sort\":8},{\"id\":2400,\"parentId\":2000,\"children\":[],\"icon\":\"icon-shouji\",\"name\":\"终端管理\",\"spread\":false,\"path\":\"/admin/client/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"终端管理\",\"sort\":9},{\"id\":2500,\"parentId\":2000,\"children\":[],\"icon\":\"icon-miyue\",\"name\":\"密钥管理\",\"spread\":false,\"path\":\"/admin/social/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"密钥管理\",\"sort\":10},{\"id\":2600,\"parentId\":2000,\"children\":[],\"icon\":\"icon-denglvlingpai\",\"name\":\"令牌管理\",\"spread\":false,\"path\":\"/admin/token/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"令牌管理\",\"sort\":11},{\"id\":2700,\"parentId\":2000,\"children\":[],\"icon\":\"icon-luyou\",\"name\":\"动态路由\",\"spread\":false,\"path\":\"/admin/route/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"动态路由\",\"sort\":12}],\"icon\":\"icon-xitongguanli\",\"name\":\"系统管理\",\"spread\":false,\"path\":\"/admin\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"系统管理\",\"sort\":1},{\"id\":3000,\"parentId\":-1,\"children\":[{\"id\":3100,\"parentId\":3000,\"children\":[],\"icon\":\"icon-server\",\"name\":\"服务监控\",\"spread\":false,\"path\":\"https://monitor.pig4cloud.com\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"服务监控\",\"sort\":0},{\"id\":3110,\"parentId\":3000,\"children\":[],\"icon\":\"icon-qingchuhuancun\",\"name\":\"缓存监控\",\"spread\":false,\"path\":\"/monitor/redis/index\",\"keepAlive\":\"1\",\"code\":null,\"type\":\"0\",\"label\":\"缓存监控\",\"sort\":1},{\"id\":3200,\"parentId\":3000,\"children\":[],\"icon\":\"icon-wendang\",\"name\":\"接口文档\",\"spread\":false,\"path\":\"https://swagger.pig4cloud.com/swagger-ui.html\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"接口文档\",\"sort\":1},{\"id\":3500,\"parentId\":3000,\"children\":[],\"icon\":\"icon-wendang\",\"name\":\"文档扩展\",\"spread\":false,\"path\":\"https://swagger.pig4cloud.com/doc.html\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"文档扩展\",\"sort\":2},{\"id\":3300,\"parentId\":3000,\"children\":[],\"icon\":\"icon-gtsquanjushiwufuwuGTS\",\"name\":\"事务监控\",\"spread\":false,\"path\":\"/tx/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"事务监控\",\"sort\":5},{\"id\":3400,\"parentId\":3000,\"children\":[],\"icon\":\"icon-online\",\"name\":\"在线事务\",\"spread\":false,\"path\":\"/tx/model\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"在线事务\",\"sort\":6},{\"id\":3630,\"parentId\":3000,\"children\":[],\"icon\":\"icon-wendang\",\"name\":\"任务日志\",\"spread\":false,\"path\":\"/daemon/execution-log/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"任务日志\",\"sort\":7},{\"id\":3600,\"parentId\":3000,\"children\":[],\"icon\":\"icon-guiji\",\"name\":\"任务轨迹\",\"spread\":false,\"path\":\"/daemon/status-trace-log/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"任务轨迹\",\"sort\":8},{\"id\":3620,\"parentId\":3000,\"children\":[],\"icon\":\"icon-gtsquanjushiwufuwuGTS\",\"name\":\"Quartz日志\",\"spread\":false,\"path\":\"/daemon/job-log/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"Quartz日志\",\"sort\":8}],\"icon\":\"icon-msnui-supervise\",\"name\":\"系统监控\",\"spread\":false,\"path\":\"/daemon\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"系统监控\",\"sort\":2},{\"id\":4000,\"parentId\":-1,\"children\":[{\"id\":4100,\"parentId\":4000,\"children\":[],\"icon\":\"icon-weibiaoti13\",\"name\":\"模型管理\",\"spread\":false,\"path\":\"/activiti/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"模型管理\",\"sort\":1},{\"id\":4200,\"parentId\":4000,\"children\":[],\"icon\":\"icon-liucheng\",\"name\":\"流程管理\",\"spread\":false,\"path\":\"/activiti/process\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"流程管理\",\"sort\":2},{\"id\":4300,\"parentId\":4000,\"children\":[],\"icon\":\"icon-qingjia\",\"name\":\"请假管理\",\"spread\":false,\"path\":\"/activiti/leave\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"请假管理\",\"sort\":3},{\"id\":4400,\"parentId\":4000,\"children\":[],\"icon\":\"icon-renwu\",\"name\":\"待办任务\",\"spread\":false,\"path\":\"/activiti/task\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"待办任务\",\"sort\":4}],\"icon\":\"icon-kuaisugongzuoliu_o\",\"name\":\"协同管理\",\"spread\":false,\"path\":\"/activti\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"协同管理\",\"sort\":3},{\"id\":6000,\"parentId\":-1,\"children\":[{\"id\":6400,\"parentId\":6000,\"children\":[],\"icon\":\"icon-anniu_weixincaidanlianjie\",\"name\":\"菜单设置\",\"spread\":false,\"path\":\"/mp/wxmenu/index\",\"keepAlive\":\"1\",\"code\":null,\"type\":\"0\",\"label\":\"菜单设置\",\"sort\":6},{\"id\":6500,\"parentId\":6000,\"children\":[],\"icon\":\"icon-zhexiantu\",\"name\":\"运营数据\",\"spread\":false,\"path\":\"/mp/wxstatistics/index\",\"keepAlive\":\"1\",\"code\":null,\"type\":\"0\",\"label\":\"运营数据\",\"sort\":7},{\"id\":6200,\"parentId\":6000,\"children\":[],\"icon\":\"icon-fensiguanli\",\"name\":\"粉丝管理\",\"spread\":false,\"path\":\"/mp/wxaccountfans/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"粉丝管理\",\"sort\":8},{\"id\":6300,\"parentId\":6000,\"children\":[],\"icon\":\"icon-xiaoxiguanli\",\"name\":\"消息管理\",\"spread\":false,\"path\":\"/mp/wxfansmsg/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"消息管理\",\"sort\":8},{\"id\":6100,\"parentId\":6000,\"children\":[],\"icon\":\"icon-weixincaidan\",\"name\":\"账号管理\",\"spread\":false,\"path\":\"/mp/wxaccount/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"账号管理\",\"sort\":8}],\"icon\":\"icon-gongzhonghao\",\"name\":\"微信管理\",\"spread\":false,\"path\":\"/mp\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"微信管理\",\"sort\":4},{\"id\":5000,\"parentId\":-1,\"children\":[{\"id\":5200,\"parentId\":5000,\"children\":[],\"icon\":\"icon-shouyintai\",\"name\":\"收银台\",\"spread\":false,\"path\":\"/pay/cd/index\",\"keepAlive\":\"1\",\"code\":null,\"type\":\"0\",\"label\":\"收银台\",\"sort\":0},{\"id\":5100,\"parentId\":5000,\"children\":[],\"icon\":\"icon-zhifuqudaoguanli\",\"name\":\"渠道管理\",\"spread\":false,\"path\":\"/pay/paychannel/index\",\"keepAlive\":\"1\",\"code\":null,\"type\":\"0\",\"label\":\"渠道管理\",\"sort\":1},{\"id\":5300,\"parentId\":5000,\"children\":[],\"icon\":\"icon-dingdan\",\"name\":\"商品订单\",\"spread\":false,\"path\":\"/pay/goods/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"商品订单\",\"sort\":2},{\"id\":5400,\"parentId\":5000,\"children\":[],\"icon\":\"icon-zhifu\",\"name\":\"支付订单\",\"spread\":false,\"path\":\"/pay/orders/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"支付订单\",\"sort\":3},{\"id\":5500,\"parentId\":5000,\"children\":[],\"icon\":\"icon-huitiao\",\"name\":\"回调记录\",\"spread\":false,\"path\":\"/pay/notify/index\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"回调记录\",\"sort\":4}],\"icon\":\"icon-pay6zhifu\",\"name\":\"支付管理\",\"spread\":false,\"path\":\"/pay\",\"keepAlive\":\"1\",\"code\":null,\"type\":\"0\",\"label\":\"支付管理\",\"sort\":4},{\"id\":9999,\"parentId\":-1,\"children\":[],\"icon\":\"icon-guanwangfangwen\",\"name\":\"系统官网\",\"spread\":false,\"path\":\"https://pig4cloud.com/#\",\"keepAlive\":\"0\",\"code\":null,\"type\":\"0\",\"label\":\"系统官网\",\"sort\":9}]";
//        JSON parse = JSONUtil.parse(json);
//        System.err.println(parse);
//        return R.success(parse.toString());
    }


    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    public R getTree() {
        return R.success(TreeUtil.buildTree(menuService.list(Wrappers.emptyWrapper()), -1L));
    }

    /**
     * 返回角色的菜单集合
     *
     * @param roleId 角色ID
     * @return 属性集合
     */
    @GetMapping("/tree/{roleId}")
    public R getRoleTree(@PathVariable Long roleId) {
        return R.success(menuService.findMenuByRoleId(roleId)
                .stream()
                .map(Menu::getId)
                .collect(Collectors.toList()));
    }

    /**
     * 通过ID查询菜单的详细信息
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return R.success(menuService.getById(id));
    }

    /**
     * 新增菜单
     *
     * @param menu 菜单信息
     * @return success/false
     */
    @SysLog("新增菜单")
    @PostMapping
    @PreAuthorize("hasAuthority('sys_menu_add')")
    public R save(@Valid @RequestBody Menu menu) {
        return R.success(menuService.save(menu));
    }

    /**
     * 删除菜单
     *
     * @param id 菜单ID
     * @return success/false
     */
    @SysLog("删除菜单")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys_menu_del')")
    public R removeById(@PathVariable Integer id) {
        return menuService.removeMenuById(id);
    }

    /**
     * 更新菜单
     *
     * @param menu
     * @return
     */
    @SysLog("更新菜单")
    @PutMapping
    @PreAuthorize("hasAuthority('sys_menu_edit')")
    public R update(@Valid @RequestBody Menu menu) {
        return R.success(menuService.updateById(menu));
    }

}

